exports.FloorUI = GL2.Node.subclass(
{
    classname: 'FloorUI',
    
    initialize: function(floorModel) 
    {
		this._background = null;
		this._floorModel = null;
		this._constructionTimeOut = null;
		this.touchCountStart = null;
		this.touchCountEnd = null;
		this._floorModel = floorModel;
    },
	
	createView: function()
	{
		this.checkAndStartProcess();
		//this.addBackground();
		//this.handleTouches();
	},
	
	addBackground: function()
	{
		var path = this.getAppropriateImagePath();
		
		if(!this._background) {
			this._background = GLFactory.getSprite([TCUtil.getAdjustedX(-78),TCUtil.getAdjustedY(0)], null, path, [TCUtil.getScreenWidth()+TCUtil.getAdjustedX(130),TCUtil.getAdjustedHeight(FLOOR_HEIGHT)]);
			this.addChild(this._background);
		} 
		else 
		{
			this._background.setImage(path,[TCUtil.getScreenWidth()+TCUtil.getAdjustedX(130),TCUtil.getAdjustedHeight(FLOOR_HEIGHT)],[0,0]);
			this._background.setPosition([TCUtil.getAdjustedX(-78),TCUtil.getAdjustedY(0)]);
		}
	},
	
	addFloorNumber: function()
	{
		var floorNumber = this._floorModel.getFloorId();
		this._floorNumber = GLFactory.getText([TCUtil.getAdjustedX(285), TCUtil.getAdjustedY(35)], 100, floorNumber, [TCUtil.getAdjustedX(30), TCUtil.getAdjustedY(60)], TCUtil.getAdjustedFontSize(180), null, null, GL2.Text.OverflowMode.ReduceFontSize, [0.0, 0.5], new Core.Color(0,0,0));
		this.addChild(this._floorNumber);		
	},
	
	handleTouches: function() 
	{
		this._touchTarget = new GL2.TouchTarget();
		this._touchTarget.setSize([TCUtil.getScreenWidth()+TCUtil.getAdjustedX(130),TCUtil.getAdjustedHeight(FLOOR_HEIGHT)]);
		this._touchTarget.setAnchor(0, 0);
		this._touchTarget.setPosition(TCUtil.getAdjustedX(-78), TCUtil.getAdjustedY(0));
		this._touchTarget.getTouchEmitter().addListener(MMGameLayer.getMallUI()._touchHandler, this.onTouch.bind(this),10);
		this._touchTarget.setDepth(100000);
		this.addChild(this._touchTarget);
	},
	
	onTouch: function(touch)
	{
		MMGameLayer.getMallUI().getTouchHandler().onTouch(touch);
		switch(touch.getAction())
		{
			case touch.Action.Start:
				this.touchCountStart = this.touchCountEnd;

                return true;
			break;
            
            case touch.Action.End:
            {				
                if(!this._isPanned || this.touchCountEnd < (this.touchCountStart + 5))
                {
					this.onFloorTouch();
                }
                this._isPanned = false;
            }
			break;
            
            case touch.Action.Move:
            	this.touchCountEnd++;	

				this._isPanned = true;
				//MMGameLayer.getMallUI().getTouchHandler().onTouch(touch);
			break;
		}		
	},
	
	onFloorTouch: function()
	{
		var item = GAGameLoader.getStoreHandler().getStoreObjectForKey(MMGameVariables.getPurchasedItemId());
		if(TCUtil.getItemType(item) == kItemType.kItemDecoration)
		{
			if(MMGameVariables.getActionType() == kActionType.kActionBuy)
			{
				//console.log("zaki::::: purchased item id is :::: "+MMGameVariables.getPurchasedItemId());
				//if(this._floorModel.getItemId() == "2_1_1")
				{
					if(MMGameVariables.getPurchasedItemId() == "2_1_1")
					{
						
					}
					else
					{
						this.setDecorationImage();
						
						MMGameVariables.setActionType(kActionType.kActionArrow);
						MMGameLayer.getControlLayer().removeActionAlertWindow();
						MMGameVariables.setPurchasedItemId(null);
					}
				}
			}
			
			else if(MMGameVariables.getActionType() == kActionType.kActionUseGift)
			{
				//console.log("zaki::::: purchased item id is :::: "+MMGameVariables.getPurchasedItemId());
				//if(this._floorModel.getItemId() == "2_1_1")
				{
					if(MMGameVariables.getPurchasedItemId() == "2_1_1")
					{
						
					}
					else
					{
						this.setDecorationImage();
						MMGameState.getCurrentUser().removeGift(MMGameVariables.getPurchasedItem());
						MMGameVariables.setActionType(kActionType.kActionArrow);
						MMGameLayer.getControlLayer().removeActionAlertWindow();
						MMGameVariables.setPurchasedItemId(null);
					}
				}
			}
		}
	},
	
	setDecorationImage: function()
	{
		this._floorModel.setItemId(MMGameVariables.getPurchasedItemId());
		var path = this.getAppropriateImagePath();
		this._background.setImage(path,[TCUtil.getScreenWidth()+TCUtil.getAdjustedX(130),TCUtil.getAdjustedHeight(FLOOR_HEIGHT)],[0,0]);
		this._floorModel.updateModel();
	},
	
	addConstructionImage: function()
	{
		if(!this._background) {
			this.addBackground();
		} 
		
		var path = this.getBaseResourcePath() + "under-construction.png";
		this._background.setImage(path,[TCUtil.getScreenWidth(),TCUtil.getAdjustedHeight(FLOOR_CONSTRUCTION_HEIGHT)],[0, 0]);
		this._background.setPosition([TCUtil.getAdjustedX(0),TCUtil.getAdjustedY(0)]);							
	},
	
	startConstruction: function()
	{
		if(this._floorModel && this._floorModel.getConstructionTime() == -1)
		{
			this._floorModel.setConstructionTime(MMGameState.getServerTime());
			this._floorModel.updateModel();
			this.checkAndStartProcess();
		}
	},
	
	checkAndStartProcess: function()
	{	
		var constructionTime = this._floorModel.getConstructionTime();
		if(constructionTime == 0) 
		{
			this.addBackground();
			
			//touch
			this.addFloorNumber();
			this.handleTouches();
		}
		else if(constructionTime == -1)
		{
			this.addConstructionImage();
		}
		else 
		{
			var remainingTime = this._floorModel.getConstructionRemainingTime();
			if(remainingTime > 0) {
				this.addConstructionImage();
				this._constructionTimeOut = setTimeout(this.constructionProgressDone.bind(this), remainingTime*1000);
			} else {
				this.constructionProgressDone();
			}
		}
	},
	
	constructionProgressDone: function()
	{
		if(this._constructionTimeOut)
		{
			clearTimeout(this._constructionTimeOut);
			this._constructionTimeOut = null;
		}
		this._floorModel.setConstructionTime(0);
		this._floorModel.updateModel();
		this.addBackground();
		//touch
		this.addFloorNumber();
		this.handleTouches();
		
		MMGameLayer.getMallUI().addDummyFloor();

		if(!TutorialManager.getHasCompletedTutorial())
		{
			if(TutorialManager.getCurrentTutorialStep() == 6)
			{	
				TutorialManager.showNextTutorialStep();
			}
		}
	},
	
	getAppropriateImagePath: function()
	{
		var itemId = this._floorModel.getItemId();
		var tempPath = itemId.split("_");
		
		return "./Content/images/storeimages/" + tempPath[0] + "/" + tempPath[1] + "/" + tempPath[2] + "/default/1.png";
	},
	
	getBaseResourcePath: function()
	{
		return "./Content/images/gamelayer/environmentlayer/";
	},
    
	destroy: function()
    {
		if(this._background) 
		{
			this._background.destroy();
			this._background = null;
		}
		
		if(this._constructionTimeOut)
		{
			clearTimeout(this._constructionTimeOut);
			this._constructionTimeOut = null;
		}
		
		this._floorModel = null;
    },
});
